import BScroll from '@better-scroll/core'
// import Vue from 'vue'

// let scroll = null

/* const setHeight = (el) => {
  const rect = el.getBoundingClientRect()
  el.style.height = rect.height
  console.log(el, rect.height, el.style.height)
  scroll.refresh()
} */

const hooks = {
  inserted (el, binding, vnode) {
    if (binding.value === null || typeof binding.value !== 'object') {
      throw new Error('The v-scroll directive must have a object about better-scroll options!')
    }
    console.log(typeof el)

    const that = vnode.context
    // 初始化 scroll，并绑定至 this
    const scroll = new BScroll(el, binding.value)
    that.scroll = scroll
    that.$nextTick(_ => {
      // console.log('now refresh scroll...')
      vnode.context.scroll.refresh()
    })
  },

  componentUpdated (el, binding, vnode) {
    console.log('v-scroll updated...')
  }
}

export default {
  name: 'scroll',
  hooks
}
